package week11;

import java.util.ArrayList;
import java.util.Scanner;

class Repo{
	private ArrayList<String> task = new ArrayList<String>();
	private volatile int flag = 0;
	
	public Repo(String items) {
		String []a=items.split(" ");
		for(int i=0;i<a.length;i++)
			task.add(a[i]);
	}
	
	public int getSize() {
		return task.size();
	}
	
	public ArrayList<String> getTask() {
		return task;
	}

	public int getFlag() {
		return flag;
	}

	public void setTask(ArrayList<String> task) {
		this.task = task;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public synchronized void task1() {
		if(getSize()>0){
			while(flag==1){
				try {
					wait();
				} catch (Exception e) {
					
				}
			
			}
			System.out.println(Thread.currentThread().getName()+" finish "+getTask().get(0));
			getTask().remove(0);
			setFlag(1);
			notifyAll();
		}
		
	}
	public synchronized void task2() {
		if(getSize()>0){
			while(flag==0){
				try {
					wait();
				} catch (Exception e) {
					
				}
			}
			System.out.println(Thread.currentThread().getName()+" finish "+getTask().get(0));
			getTask().remove(0);
			setFlag(0);
			notifyAll();
		}
		
	}
}
class Worker1 implements Runnable{
	private Repo repo;
	private int length;
	
	public Worker1(Repo repo) {
		this.repo=repo;
		this.length=repo.getSize();
	}

	public synchronized void run() {
		for(int i=0;i<length;i++)
			repo.task1();
	}
	
}
class Worker2 implements Runnable{
	private Repo repo;
	private int length;
	
	public Worker2(Repo repo) {
		this.repo=repo;
		this.length=repo.getSize();
	}

	public synchronized void run() {
		for(int i=0;i<length;i++)
			repo.task2();
	}
	
}
public class Main6 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        Repo repo = new Repo(sc.nextLine());
        Thread t1 = new Thread(new Worker1(repo));
        Thread t2 = new Thread(new Worker2(repo));
        t1.start();
        Thread.yield();
        t2.start();
        sc.close();
	}

}
